Skip to content

sysupgrade: add NAND flash support#2012

Closed
widgetii wants to merge 2 commits into
masterfrom
sysupgrade-nand-support
Closed

sysupgrade: add NAND flash support#2012
widgetii wants to merge 2 commits into
masterfrom
sysupgrade-nand-support

Conversation

@widgetii

@widgetii widgetii commented Apr 16, 2026

Copy link
Copy Markdown
Member

Summary

  • sysupgrade previously hardcoded NOR/squashfs firmware downloads and used flashcp for all flash types, which bricks NAND cameras (boot expects ubifs, not squashfs)
  • download_firmware() now selects the correct NAND package ($soc-nand-$osr) when ipcinfo -F reports nand
  • do_update_kernel() uses flash_eraseall + nandwrite -p for NAND instead of flashcp
  • New do_update_rootfs_nand() function: pivot_roots to tmpfs, detaches UBI, erases the MTD partition, writes the UBI image with nandwrite, and reboots
  • do_wipe_overlay() uses ubiupdatevol -t for NAND overlay wipe instead of flash_eraseall -j (JFFS2-specific)

Known limitation

NAND rootfs update erases the entire UBI partition, which includes both rootfs and rootfs_data (overlay). This means overlay data (settings, SSH keys, etc.) is lost during rootfs update. Preserving overlay would require ubiupdatevol with a bare UBIFS image, which is not currently included in release packages. Re-attaching UBI after nandwrite to restore overlay data is unsafe — it causes image_seq mismatch and kernel panic.

Test log (hi3516av200, 128MB SPI NAND)

sysupgrade -k -r -f -z output
OpenIPC System Updater v1.0.48

Vendor  hisilicon
SoC     hi3516av200
Kernel  23:23:49 2026-04-15
RootFS  master+1783691, 2026-04-15

Stop services, sync files, free up memory
Stopping crond: OK
Stopping ntpd: OK
Stopping klogd: OK
Stopping syslogd: OK

Firmware
Download from https://github.com/OpenIPC/firmware/releases/download/latest/openipc.hi3516av200-nand-ultimate.tgz
Received and unpacked

Kernel
Update kernel from /tmp/uImage.hi3516av200
Erasing kernel partition...
Erasing 128 Kibyte @ 300000 - 100% complete.
Writing kernel...
Writing at 0x001c0000
Kernel updated to 11:17:22 2026-04-16

RootFS
Update rootfs (NAND) from /tmp/rootfs.ubi.hi3516av200
UBI partition: /dev/mtd3 (mtd3)
Preparing tmpfs root for NAND upgrade...
Performing pivot_root to tmpfs and flashing NAND...
Unmounting old root filesystems...
Detaching UBI from mtd3...
Erasing /dev/mtd3...
Erasing 128 Kibyte @ 7c00000 - 100% complete.
Writing rootfs.ubi to /dev/mtd3...
Writing at 0x00c00000
NAND upgrade complete. Rebooting...
Post-upgrade verification
--- os-release ---
OPENIPC_VERSION=2.6.04.16
GITHUB_VERSION="master+22307c9, 2026-04-16"
BUILD_OPTION=ultimate

--- MTD partitions ---
mtd0: 00040000 00020000 "boot"
mtd1: 000c0000 00020000 "wtf"
mtd2: 00300000 00020000 "kernel"
mtd3: 07c00000 00020000 "ubi"
mtd4: 02017000 0001f000 "rootfs"
mtd5: 05521000 0001f000 "rootfs_data"

--- Mount points ---
ubi0:rootfs on /rom type ubifs (ro,relatime)
ubi0:rootfs_data on /overlay type ubifs (rw,relatime)
overlay on / type overlay (rw,relatime,...)

--- Disk usage ---
ubi0:rootfs    27.8M  9.2M  18.6M  33% /rom
ubi0:rootfs_data 76.8M 28.0K 72.8M  0% /overlay

--- Uptime ---
 11:27:24 up 1 min

Test plan

  • Tested on hi3516av200 with 128MB SPI NAND — upgrade from OpenIPC 2.2 (Nov 2022) to 2.6 (Apr 2026) succeeded
  • Tested second upgrade on same board — confirmed pivot_root works reliably
  • Verified overlay is erased (clean /overlay after upgrade)
  • Test on other NAND-based boards
  • Test NOR cameras are unaffected (no code path changes for NOR)
  • Test standalone overlay wipe (sysupgrade -n) on NAND

🤖 Generated with Claude Code

The sysupgrade script previously hardcoded NOR/squashfs firmware
downloads and used flashcp for all flash types. This bricks NAND
cameras which boot with rootfstype=ubifs, since flashing squashfs
to a UBI volume produces an unmountable rootfs.

Changes:
- download_firmware: select nand package when ipcinfo -F reports nand
- do_update_kernel: use flash_eraseall + nandwrite for NAND partitions
- do_update_rootfs_nand: new function that pivot_roots to tmpfs,
  detaches UBI, erases the MTD partition, and writes the UBI image
  with nandwrite before rebooting
- do_wipe_overlay: use ubiupdatevol -t for NAND overlay wipe instead
  of flash_eraseall -j which is JFFS2-specific

Tested on hi3516av200 with 128MB SPI NAND, upgrading from OpenIPC
2.2 (Nov 2022) to 2.6 (Apr 2026) successfully.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Re-attaching UBI after nandwrite to restore overlay data causes
image_seq mismatch — the rootfs_data autoresize writes PEBs with
a different sequence number than the original rootfs.ubi image,
resulting in kernel panic on boot (UBI attach fails).

Add a warning that NAND rootfs update erases overlay data.
Overlay preservation would require ubiupdatevol with a bare UBIFS
image, which is not currently included in release packages.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@ZigFisher ZigFisher left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will break sigmastar nand devices, the old solution was utilizing gluebi as compatibility layer to overwrite the partitions without ubi tools.

@widgetii

Copy link
Copy Markdown
Member Author

Are you comfortable making these changes only for HiSilicon devices, which I can currently test?

@widgetii

Copy link
Copy Markdown
Member Author

Superseded by #2022, which is a comprehensive rework that addresses the data-loss issue called out in review:

  • Switches HiSilicon NAND firmware to SquashFS-on-UBI layout (matches sigmastar/rockchip).
  • Replaces flash_eraseall + nandwrite with ubiformat -y -f so the autoresize/image_seq trap that triggered the kernel panic on this branch is avoided.
  • Preserves user data across upgrades by default via the OpenWrt nand.sh pattern (tarball staged in rootfs_data, extracted by /init on first boot).
  • Idempotent fw_setenv bootargs migration runs after the flash succeeds.

See #2022 for the full design rationale and upgrade steps.

@widgetii widgetii closed this Apr 26, 2026
widgetii pushed a commit that referenced this pull request Jun 19, 2026
Existing NAND sysupgrade was destructive: every -r wiped the entire UBI
MTD and the overlay with it. Adopt OpenWrt's nand.sh pattern and let
HiSilicon NAND survive upgrades the same way NOR does.

Flow on a NAND camera with ubiformat available (= post-this-PR firmware):

  1. Build a tarball of /etc/passwd, /etc/dropbear, /etc/network,
     /etc/majestic.yaml, /root, ... — defaults plus anything in
     /etc/sysupgrade.conf or /lib/upgrade/keep.d/*.
  2. Compute migrated bootargs (root=/dev/ubiblock0_0 rootfstype=squashfs
     ubi.block=0,0 ubi.mtd=N,2048). Idempotent — empty result if already
     migrated.
  3. Pivot to a tmpfs new-root carrying busybox, ubiformat, fw_setenv,
     /etc/fw_env.config, the new rootfs.ubi, and the user-data tarball.
  4. ubidetach + 'ubiformat -y -f rootfs.ubi /dev/mtdN'. ubiformat is
     the only primitive that handles erase-counter preservation and
     writes a single fresh image_seq across every PEB, including the
     ones autoresize will later claim. nandwrite fails this on reboot
     with 'bad image sequence number' panics.
  5. fw_setenv bootargs (post-flash, so a flash failure cannot brick
     the env).
  6. ubiattach, wait for autoresize to materialise rootfs_data, mount
     it, drop /sysupgrade.tgz, ubidetach, reboot.

On first boot of the new firmware /init mounts rootfs_data, finds the
tarball, extracts it onto the overlay upper, removes the file, and
proceeds normally.

Existing UBIFS-rootfs cameras have no ubiformat. For that one-time
hop we fall back to flash_eraseall+nandwrite (the path PR #2012
introduced) and warn that user data is erased; subsequent upgrades on
the new firmware preserve it.

The previous PR-#2012 'overlay erased' warning is dropped — overlay is
preserved by default now. -n / --wipe_overlay opts in to wiping (it
skips the tarball step so rootfs_data is left empty after flash).

Other changes:

- general/overlay/init: regex broadened to also enter the UBI overlay
  branch on squashfs+ubi.block= cmdlines, and a one-shot
  /overlay/sysupgrade.tgz extractor added. The tarball is rm -f'd
  after extraction so subsequent boots are no-ops.
- general/overlay/etc/sysupgrade.conf: new empty file so users can
  drop additional paths to preserve without modifying the upgrader.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
widgetii pushed a commit that referenced this pull request Jun 19, 2026
Existing NAND sysupgrade was destructive: every -r wiped the entire UBI
MTD and the overlay with it. Adopt OpenWrt's nand.sh pattern and let
HiSilicon NAND survive upgrades the same way NOR does.

Flow on a NAND camera with ubiformat available (= post-this-PR firmware):

  1. Build a tarball of /etc/passwd, /etc/dropbear, /etc/network,
     /etc/majestic.yaml, /root, ... — defaults plus anything in
     /etc/sysupgrade.conf or /lib/upgrade/keep.d/*.
  2. Compute migrated bootargs (root=/dev/ubiblock0_0 rootfstype=squashfs
     ubi.block=0,0 ubi.mtd=N,2048). Idempotent — empty result if already
     migrated.
  3. Pivot to a tmpfs new-root carrying busybox, ubiformat, fw_setenv,
     /etc/fw_env.config, the new rootfs.ubi, and the user-data tarball.
  4. ubidetach + 'ubiformat -y -f rootfs.ubi /dev/mtdN'. ubiformat is
     the only primitive that handles erase-counter preservation and
     writes a single fresh image_seq across every PEB, including the
     ones autoresize will later claim. nandwrite fails this on reboot
     with 'bad image sequence number' panics.
  5. fw_setenv bootargs (post-flash, so a flash failure cannot brick
     the env).
  6. ubiattach, wait for autoresize to materialise rootfs_data, mount
     it, drop /sysupgrade.tgz, ubidetach, reboot.

On first boot of the new firmware /init mounts rootfs_data, finds the
tarball, extracts it onto the overlay upper, removes the file, and
proceeds normally.

Existing UBIFS-rootfs cameras have no ubiformat. For that one-time
hop we fall back to flash_eraseall+nandwrite (the path PR #2012
introduced) and warn that user data is erased; subsequent upgrades on
the new firmware preserve it.

The previous PR-#2012 'overlay erased' warning is dropped — overlay is
preserved by default now. -n / --wipe_overlay opts in to wiping (it
skips the tarball step so rootfs_data is left empty after flash).

Other changes:

- general/overlay/init: regex broadened to also enter the UBI overlay
  branch on squashfs+ubi.block= cmdlines, and a one-shot
  /overlay/sysupgrade.tgz extractor added. The tarball is rm -f'd
  after extraction so subsequent boots are no-ops.
- general/overlay/etc/sysupgrade.conf: new empty file so users can
  drop additional paths to preserve without modifying the upgrader.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants